package base.thread_JUC2.chapter02.demo04;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintQueue {
    private final Lock queueLock = new ReentrantLock();

    public void printJob(Object o) {
        queueLock.lock();
        try {
            long duration = (long) (Math.random() * 10);
            System.out.printf("%s PrintQueue printing a job during %d seconds \n", Thread.currentThread().getName(),duration);
            TimeUnit.SECONDS.sleep(duration);
//            System.out.printf("我是 %s， 来咬我呀，那是不可能的 \n", Thread.currentThread().getName());
        }catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            queueLock.unlock();
        }//出了这里，可能立马下一个线程就 PrintQueue printing a job during X seconds，导致错觉是两个线程同时进入 queueLock.lock() 控制的同步块

    }
}
